package org.budo.jdbc.dao.resultset.handler;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.budo.jdbc.dao.ResultSetHandler;
import org.budo.support.javax.sql.util.ResultSetUtil;

/**
 * @author lmw
 */
public abstract class AbstractListResultSetHandler<T> implements ResultSetHandler<List<T>> {
    @Override
    public List<T> handleResultSet(ResultSet resultSet) {
        if (null == resultSet) {
            return null;
        }

        List<T> list = new ArrayList<T>();

        while (ResultSetUtil.next(resultSet)) {
            T row = this.handleResultSetRow_1(resultSet);
            list.add(row);
        }

        return list;
    }

    private T handleResultSetRow_1(ResultSet resultSet) {
        try {
            return this.handleResultSetRow(resultSet);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    public abstract T handleResultSetRow(ResultSet resultSet) throws Throwable;
}